package com.exp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exp.dto.DeptPageQueryDTO;
import com.exp.mapper.DeptInfMapper;
import com.exp.result.PageResult;
import com.exp.service.DeptInfService;
import com.exp.pojo.DeptInf;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 王-自信
* @description 针对表【dept_inf】的数据库操作Service实现
* @createDate 2024-11-18 20:52:04
*/
@Service
public class DeptInfServiceImpl extends ServiceImpl<DeptInfMapper, DeptInf>
    implements DeptInfService {
    @Autowired
    private DeptInfMapper deptInfMapper;

    /**
     * 分页查询
     */

    @Override
    public PageResult pageQuery(DeptPageQueryDTO depPageQueryDTO) {
        // select * from employee limit 0,10
        //开始分页查询
        PageHelper.startPage(depPageQueryDTO.getPage(), depPageQueryDTO.getPageSize());

        Page<DeptInf> page = deptInfMapper.pageQuery(depPageQueryDTO);

        long total = page.getTotal();
        List<DeptInf> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    public void update(DeptInf deptInf) {
        deptInfMapper.update(deptInf);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        //判断是否能够删除---是否被员工表关联
        //根据部门id 集合 查询对应的员工id 集合
        List<Integer> employeeIds = deptInfMapper.getEmployeeIdsByDeptIds(ids);
        //System.out.println("根据部门id 集合 查询对应的员工id 集合"+employeeIds);

        if (employeeIds != null && employeeIds.size() > 0) {
            //被关联了，不能删除
            throw new RuntimeException("当前用户被employee表关联了，不能删除");
        }

        //删除数据
        for (Integer id : ids) {
            deptInfMapper.deleteById(id);
        }
    }


}




